//
//  CustomControls.m
//  AMT media manager
//
//  Created by gift ku on 2/5/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CustomControls.h"


@implementation CustomSliderCell

- (id)copyWithZone:(NSZone*)zone
{
    CustomSliderCell* cell = [super copyWithZone:zone];
    cell->_knobOffset         = _knobOffset;
    cell->_trackOffset        = _trackOffset;
    cell->_backColor          = [_backColor retain];
    cell->_knobImage          = [_knobImage retain];
    cell->_knobImagePressed   = [_knobImagePressed retain];
    cell->_knobImageDisabled  = [_knobImageDisabled retain];
    cell->_trackImage         = [_trackImage retain];
    cell->_trackImageDisabled = [_trackImageDisabled retain];
	cell->_trackImageCover    = [_trackImageCover retain];
    return cell;
}

- (void)dealloc
{
    [_backColor release];
    [_knobImage release];
    [_knobImagePressed release];
    [_knobImageDisabled release];
    [_trackImage release];
    [_trackImageDisabled release];
	[_trackImageCover release];
    [super dealloc];
}

- (void)setImageName:(NSString*)imageName backColor:(NSColor*)backColor
         trackOffset:(float)trackOffset knobOffset:(float)knobOffset
{
    _trackImage         = imageNamedWithPostfix(imageName, @"SliderTrack");
    _trackImageDisabled = imageNamedWithPostfix(imageName, @"SliderTrackDisabled");
    if (!_trackImageDisabled) {
        _trackImageDisabled = [_trackImage retain];
    }
	_trackImageCover    = imageNamedWithPostfix(imageName, @"SliderTrackCover");
	if(!_trackImageCover){
		_trackImageCover = [_trackImageCover retain];
	}
    _knobImage          = imageNamedWithPostfix(imageName, @"SliderKnob");
    _knobImagePressed   = imageNamedWithPostfix(imageName, @"SliderKnobPressed");
    if (!_knobImagePressed) {
        _knobImagePressed = [_knobImage retain];
    }
    _knobImageDisabled  = imageNamedWithPostfix(imageName, @"SliderKnobDisabled");
    if (!_knobImageDisabled) {
        _knobImageDisabled = [_knobImage retain];
    }
    _backColor = [backColor retain];
    _trackOffset = trackOffset;
    _knobOffset = knobOffset;
}

- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped
{
    NSImage* track = ([self isEnabled]) ? _trackImage : _trackImageDisabled;
	NSImage* trackCover = _trackImageCover;
	
    rect.origin.x -= ([_trackImage size].width - rect.size.width) / 2;
    rect.origin.y += _trackOffset;
    if (_backColor) {   // hide original drawing...
        [_backColor set];
        if (0 < [self numberOfTickMarks]) {
            NSRect rc = rect;
            rc.size.height = 5;     // except tick marks
            NSRectFill(rc);
        }
        else {
            NSRectFill(rect);
        }
    }
    [track setFlipped:flipped];
    [track drawAtPoint:rect.origin fromRect:NSZeroRect
             operation:NSCompositeSourceOver fraction:1.0];
	NSInteger number = rect.size.width * [self floatValue] / ([self maxValue] - [self minValue]);
	rect.size.width = number;
	rect.size.height = 5;
	[trackCover drawInRect:rect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
	NSLog(@"%d",number);
}

- (NSRect)knobRectFlipped:(BOOL)flipped
{
    NSRect rect = [super knobRectFlipped:flipped];
    rect.origin.x -= ([_knobImage size].width - rect.size.width) / 2;
    rect.origin.y += _knobOffset;
    return rect;
}

- (void)drawKnob:(NSRect)knobRect
{
    NSImage* knob = (![self isEnabled]) ? _knobImageDisabled :
	(_scFlags.isPressed) ? _knobImagePressed : _knobImage;
    [knob setFlipped:TRUE];
    [knob drawAtPoint:knobRect.origin fromRect:NSZeroRect
            operation:NSCompositeSourceOver fraction:1.0];
}

@end